package com.example.springboothibernate.controller;

import com.example.springboothibernate.entity.User;
import com.example.springboothibernate.service.UserService;
import com.example.springboothibernate.unitls.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/getByNameAndAge")
    @ResponseBody
    public Result getByNameAndAge(String name,int age){
        return userService.getByNameAndAge(name,age);
    }

    @GetMapping("/queryAll")
    public Result queryAll(){
        return userService.queryAll();
    }

    @GetMapping("/getById")
    public Result getById(){
        return userService.getById();
    }

    @PostMapping("/save")
    public Result addUser(String name,int age){
        User user = new User();
        user.setAge(age);
        user.setName(name);
        return userService.addUser(user);
    }

    @PostMapping("/delete")
    public Result deleteUser(Integer id){
        return userService.deleteUser(id);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        return  userService.findAll();
    }

    @PostMapping("/update")
    public Result updateUser(User user){
        Integer age = user.getAge();
        Integer id = user.getId();
        String name = user.getName();
        return userService.updateById(user);
    }

}
